﻿var rangeData=[{name:"已收消息",code:"1"},{name:"未读消息",code:"0"},{name:"已发消息",code:"2"}];
var messageTypeData;


//初始化入口
$(function() {
    $('#dlg-addMessage').dialog('close');
    $('#dlg-searchMessage').dialog('close');
    $('#dlg-AnswerMessage').dialog('close');
    $('#searchrange').combobox('loadData', rangeData);
    $('#messageGrid').datagrid('options').onSelect = function(rowIndex, rowData) {
        $('#messageContextDiv').empty();
        $('#messageContextDiv').load("MessageContext.aspx", {}, function() {
            $('#messageCtitle').append(rowData.TitleText);
            $('#messageCsenduser').append(rowData.SendUserName);
            $('#messageCrelationtext').attr('href', rowData.Relation_URL);
            $('#messageCrelationtext').append(rowData.Relation_Text);
            $('#messageCtext').append(rowData.ContentText);
        });

        requestAjax('Controller.aspx?controller=MessageController&method=ReadMessage', { messageId: rowData.Id });
    }
    $.getJSON("Controller.aspx?controller=MessageController&method=GetMessageTypeData", function(json){
        messageTypeData=json;
        pageInit();
    });
});

function pageInit() {
    if (MessageId) {
        $('#messageGrid').datagrid('options').url = 'Controller.aspx?controller=MessageController&method=GetMessageData&type=-1&MessageId=' + MessageId;
        $('#messageGrid').datagrid('reload');
        $('#messageGrid').datagrid('options').onLoadSuccess = function(data) {
            $('#messageGrid').datagrid('selectRow', 0);
        }
    }
}

function tree_getmessage(type) {
    $('#messageGrid').datagrid('options').url = 'Controller.aspx?controller=MessageController&method=GetMessageData&type=' + type;
    $('#messageGrid').datagrid('reload');
    
}

function btn_searchMessage() {
    $('#dlg-searchMessage').dialog('open');
}

function dlg_searchMessageOk() {
    var type = $('#searchrange').combobox('getValue');
    var messagetype = $('#searchmessagetype').combobox('getValue');
    $('#messageGrid').datagrid('options').url = 'Controller.aspx?controller=MessageController&method=GetMessageData&type=' + type + '&messagetype=' + messagetype;
    $('#messageGrid').datagrid('reload');
    $('#dlg-searchMessage').dialog('close');
}

function btn_reloadMessage() {
    $('#messageGrid').datagrid('reload');
}

function btn_addMessage() {
    $('#dlg-addMessage').dialog({ title: '新增消息' });
    $('#formaddmessage').form('clear');
    $('#formmessagetypecode').combobox('setValue','06');
    $('#formmessagetypecode').combobox('disable');
    $('#formmessagetypecode1').val('06');
}

function btn_answerMessage() {
    var selected = $('#messageGrid').datagrid('getSelected');
    if (selected) {
        $('#dlg-AnswerMessage').dialog('open');
        $('#formanswermessage').form('clear');
        //$('#formanswermessage').form('load', selected);
        $('#formanswermessageTitleText').val('回复：' + selected.TitleText);
        $('#formanswermessageContentText').val('\n\n\n------------------------\n' + selected.ContentText);
        $('#formAnswerAnswer_MessageId').val(selected.Id);

        $('#formanswermessageTypeCode').val(selected.TypeCode);
        $('#formanswermessageReceiveWorkId').val(selected.SendWorkId);
        $('#formanswermessageReceiveDeptId').val(selected.SendDeptId);
        $('#formanswermessageReceiveUserId').val(selected.SendUserId);
    }
}

function dlg_addMessageOk() {
    FormSubmit($('#formaddmessage'), 'Controller.aspx?controller=MessageController&method=SaveMessage', function() {
        $('#dlg-addMessage').dialog('close');
        btn_reloadMessage();
    });
}

function dlg_AnswerMessageOk() {
    FormSubmit($('#formanswermessage'), 'Controller.aspx?controller=MessageController&method=SaveMessage', function() {
        $('#dlg-AnswerMessage').dialog('close');
        
    });
}

function textFormatter(value){
    if(messageTypeData){
        for(var i=0;i<messageTypeData.length;i++){
            if(messageTypeData[i].CODE==value)
            return messageTypeData[i].NAME;
        }
    }
    return '';
}